package com.smt.modules.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.smt.common.base.BaseController;
import com.smt.modules.bus.vo.AreaVo;
import com.smt.modules.bus.entity.AreaEntity;
import com.smt.modules.bus.service.AreaService;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.R;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;


/**
 * 
 *
 * @author murong
 * @email 981620741@qq.com
 * @date 2018-09-30 10:03:48
 */
@RestController
@RequestMapping("/app/area")
@Api(tags = {"AppAreaController"},description = "行政规划表(客户端接口)")
public class AppAreaController extends BaseController{
    @Autowired
    private AreaService areaService;

    /**
    * 查询分页方法
    * @param params
    * @return
    */
    @ApiIgnore
    @PostMapping("/list")
    @ApiOperation(value = "/list",notes = "根据条件查询",response = AreaVo.class)
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = areaService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 省市区查询
     * @param levelType
     * @param parentId
     * @return
     */
    @PostMapping("/listArea")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "levelType",value = "等级(1省级，2，市级 3,区级)",required = true,example="1",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name="parentId",value = "父级id(一级：100000)",dataType = "Long",paramType = "query"),
    })
    @ApiOperation(value = "/listArea",notes = "根据条件查询省市区",response = AreaVo.class)
    public R listArea(Integer levelType,@NotNull(message = "父id必填") Long parentId){
        List<AreaVo> areaVos = areaService.queryArea(levelType,parentId);

        return R.ok().put("area",areaVos);
    }




    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "/info/{id}",notes = "根据表ID查询",response = AreaVo.class)
    public R info(@PathVariable("id") Long id){
			AreaEntity area = areaService.selectById(id);

        return R.ok().put("area", area);
    }

    /**
     * 保存
     */
    @ApiIgnore
    @PostMapping("/save")
    @ApiOperation(value = "/save",notes = "添加信息保存",response = AreaVo.class)
    public R save(@RequestBody AreaEntity area){
			areaService.insert(area.copyPropertiesTo(new AreaEntity()));

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @PostMapping("/update")
    @ApiOperation(value = "/update",notes = "更新信息保存",response = AreaVo.class)
    public R update(@RequestBody AreaEntity area){
			areaService.updateById(area.copyPropertiesTo(new AreaEntity()));

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @DeleteMapping("/delete")
    @ApiOperation(value = "/delete",notes = "批量删除",response = AreaVo.class)
    public R delete(@RequestBody Long[] ids){
			areaService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
